<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Shoping extends CI_Controller {
	public function __construct(){
		parent::__construct();
		if(!$this->session->userdata('user_is_login')){
			redirect('user/login');
		}
	}	
	public function add()
	{	
		$prodect_id=$this->input->post('prodect_id');
		$user_id=$this->session->userdata('user_id');
		$num=$this->input->post('num');
		$this->load->model('User_cart_model');
		
		if(!$this->User_cart_model->get_by_user($prodect_id,$user_id)){
			$this->User_cart_model->get_add_shopcart($prodect_id,$user_id,$num);
		}else{
			$this->User_cart_model->update_shopcart($prodect_id,$user_id,$num);
		}
		
		redirect('prodect/detail/'.$prodect_id);
	}
	public function shopcart(){
		$this->load->model('User_cart_model');
		$this->load->model('Prodect_model');
		$user_id=$this->session->userdata('user_id');
		$data=array(
			'cart'=>$this->User_cart_model->get_user($user_id),
			);

		$this->load->view('shopcart',$data);
	}
	public function del($id){
		$this->load->model('User_cart_model');
		$this->User_cart_model->del($id);
		redirect('shoping/shopcart/');
	}
	public function delcart(){
		$this->load->model('User_cart_model');
		$ids=$this->input->post("ids");
		$this->User_cart_model->delcart($ids);
		echo "ok";
	}
	public function update(){
		$id=$this->input->post('id');
		$num=$this->input->post('num');
		if ($num>0) {
			$this->load->model('User_cart_model');
			$this->User_cart_model->update($id,$num);
			echo "ok";
		}else{
			echo "商品件数必须大于等于1件";
		}
	}
	public function preorder(){
		$data=$this->input->post('data');
		if(count($data)==0){
			echo "请至少选中一件商品";
		}else{
			$this->session->set_userdata('data',$data);
			echo "ok";
		}
		
	}
	public function createorder(){
		if($this->input->post()){
			$address_id=$this->input->post('address_id');
			$user_id=$this->session->userdata('user_id');
			$this->load->model("Order_model");
			$order_id=$this->Order_model->creat_one_order($address_id,$user_id);
			$prodect_ids=$this->input->post('prodect_id');
			$num=$this->input->post('num');
			$count=count($prodect_ids);
			for($i=0;$i<$count;$i++){
				$this->Order_model->add_prodect_to_order($prodect_ids[$i],$num[$i],$order_id);
			}

		};
		$this->load->model('Prodect_model');
		$dat=$this->session->userdata('data');
		$this->load->model('User_addres_model');
		$user_id=$this->session->userdata('user_id');
		$address=$this->User_addres_model->get_addres_by_user_id($user_id);
		$data=array(
			'da'=>$dat,
			'address'=>$address,
		);
		
		$this->load->view('createorder',$data);

	}
	public function saveaddress(){
		$name=$this->input->post('name');
		$address=$this->input->post('address');
		$phone=$this->input->post('phone');
		if(empty($name)){
			echo json_encode(array('code'=>'error','field'=>'name','msg'=>'名字不能为空'));
			exit();
		};
		if(empty($address)){
			echo json_encode(array('code'=>'error','field'=>'address','msg'=>'地址不能为空'));
			exit();
		};
		if(empty($phone)){
			echo json_encode(array('code'=>'error','field'=>'phone','msg'=>'号码不能为空'));
			exit();
		};
		$this->load->model('User_addres_model');
		$user_id=$this->session->userdata('user_id');
		$this->User_addres_model->saveaddress($name,$address,$phone,$user_id);
		echo json_encode(array('code'=>'ok'));
	}

}